package com.yunshan.welfare.service.impl;

import com.yunshan.common.utils.DateUtils;
import com.yunshan.welfare.domain.WelfareCoupon;
import com.yunshan.welfare.domain.WelfareGift;
import com.yunshan.welfare.domain.WelfareGiftContent;
import com.yunshan.welfare.domain.WelfareGiftRecord;
import com.yunshan.welfare.mapper.WelfareGiftMapper;
import com.yunshan.welfare.service.IWelfareCouponService;
import com.yunshan.welfare.service.IWelfareGiftContentService;
import com.yunshan.welfare.service.IWelfareGiftRecordService;
import com.yunshan.welfare.service.IWelfareGiftService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 礼包管理Service业务层处理
 *
 * @author yunshan
 * @date 2025-01-20
 */
@Service
public class WelfareGiftServiceImpl implements IWelfareGiftService
{
    @Autowired
    private WelfareGiftMapper welfareGiftMapper;
    @Autowired
    private IWelfareGiftContentService welfareGiftContentService;
    @Autowired
    private IWelfareGiftRecordService welfareGiftRecordService;
    @Autowired
    private IWelfareCouponService welfareCouponService;

    /**
     * 查询礼包管理
     *
     * @param id 礼包管理主键
     * @return 礼包管理
     */
    @Override
    public WelfareGift selectWelfareGiftById(Long id)
    {
        WelfareGift welfareGift = welfareGiftMapper.selectWelfareGiftById(id);

        if (welfareGift == null){
            return null;
        }

        //礼包内容
        WelfareGiftContent welfareGiftContent = new WelfareGiftContent();
        welfareGiftContent.setGiftId(id);
        List<WelfareGiftContent> welfareGiftContents = welfareGiftContentService.selectWelfareGiftContentList(welfareGiftContent);
        if (welfareGiftContents.size() > 0){
            welfareGift.setWelfareGiftContent(welfareGiftContents);
        }

        return welfareGift;
    }

    /**
     * 查询礼包管理列表
     *
     * @param welfareGift 礼包管理
     * @return 礼包管理
     */
    @Override
    public List<WelfareGift> selectWelfareGiftList(WelfareGift welfareGift)
    {
        List<WelfareGift> welfareGifts = welfareGiftMapper.selectWelfareGiftList(welfareGift);

        //礼包发放数量
        if (welfareGifts .size() > 0){
            for (WelfareGift gift : welfareGifts) {
                int size = welfareGiftRecordService.selectWelfareGiftRecordCount(gift.getId());
                gift.setNum(size);
            }
        }
        return welfareGifts;
    }

    /**
     * 新增礼包管理
     *
     * @param welfareGift 礼包管理
     * @return 结果
     */
    @Override
    public int insertWelfareGift(WelfareGift welfareGift)
    {
        welfareGift.setCreateTime(DateUtils.getNowDate());
        welfareGiftMapper.insertWelfareGift(welfareGift);

        //新增礼包内容
        welfareGift.setCreateTime(null);
        List<WelfareGift> welfareGifts = selectWelfareGiftList(welfareGift);
        WelfareGift gift = welfareGifts.get(0);
        List<WelfareGiftContent> welfareGiftContent = welfareGift.getWelfareGiftContent();
        if (welfareGiftContent.size() > 0){
            for (WelfareGiftContent giftContent : welfareGiftContent) {
                giftContent.setGiftId(gift.getId());
                //存入优惠券名字
                WelfareCoupon welfareCoupon = welfareCouponService.selectWelfareCouponById(giftContent.getContentId());
                giftContent.setContentName(welfareCoupon.getName());
                giftContent.setCreateTime(DateUtils.getNowDate());
                welfareGiftContentService.insertWelfareGiftContent(giftContent);
            }
        }

        return 1;
    }

    /**
     * 修改礼包管理
     *
     * @param welfareGift 礼包管理
     * @return 结果
     */
    @Override
    public int updateWelfareGift(WelfareGift welfareGift)
    {
        //如果礼包内容不为空去更新礼包内容
        if (welfareGift.getWelfareGiftContent().size() > 0) {
            welfareGiftContentService.deleteWelfareGiftContentByGiftId(welfareGift.getId());
            List<WelfareGiftContent> welfareGiftContent = welfareGift.getWelfareGiftContent();
            if (welfareGiftContent.size() > 0) {
                for (WelfareGiftContent giftContent : welfareGiftContent) {
                    //存入优惠券名字
                    WelfareCoupon welfareCoupon = welfareCouponService.selectWelfareCouponById(giftContent.getContentId());
                    giftContent.setContentName(welfareCoupon.getName());

                    giftContent.setGiftId(welfareGift.getId());
                    giftContent.setCreateTime(DateUtils.getNowDate());
                    welfareGiftContentService.insertWelfareGiftContent(giftContent);
                }
            }
        }
        return welfareGiftMapper.updateWelfareGift(welfareGift);
    }

    /**
     * 批量删除礼包管理
     *
     * @param ids 需要删除的礼包管理主键
     * @return 结果
     */
    @Override
    public int deleteWelfareGiftByIds(Long[] ids)
    {
        return welfareGiftMapper.deleteWelfareGiftByIds(ids);
    }

    /**
     * 删除礼包管理信息
     *
     * @param id 礼包管理主键
     * @return 结果
     */
    @Override
    public int deleteWelfareGiftById(Long id)
    {
        return welfareGiftMapper.deleteWelfareGiftById(id);
    }
}
